package de.clapp.client.musicbox;

import java.util.ArrayList;
import java.util.List;

public class MyCircularArray<T>
{
    private int size, pointer;

    private Object[] array;

    public MyCircularArray(int size)
    {
        this.size = size;
        this.array = new Object[size];
    }

    public void add(T item)
    {
        this.array[pointer] = item;
        pointer++;
        pointer %= size;
    }

    public List<T> getAll()
    {
        ArrayList<T> items = new ArrayList<T>();
        int localPointer = this.pointer - 1;

        if (localPointer >= 0)
        {
            for (int i = 0; i < this.size; i++)
            {
                if (this.array[localPointer] != null)
                {
                    items.add((T) this.array[localPointer]);
                    localPointer--;
                    localPointer += size;
                    localPointer %= size;
                }
            }
        }

        return items;
    }
}
